var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    commentList: [],
    //评论内容
    content: '',
    //弹出键盘
    focus: false,
  },
  onLoad: function(options) {
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15,
      windowHeight: app.globalData.systemInfo.windowHeight,
    })
    this.getCommentList("",false);
  },
  onReachBottom: function() {
    var commentList = this.data.commentList;
    var time = commentList[commentList.length - 1].messageDate;
    this.getCommentList(time,true);
  },
  talkInfo: function(e) {
    var index = e.currentTarget.dataset.index;
    var id = this.data.commentList[index].extent2;
    wx.navigateTo({
      url: '/pages/talkInfo/talkInfo?id=' + id
    })
  },
  //获取评论列表
  getCommentList: function (time,reach) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getCommentMessageList',
      data: {
        time: time,
        userId: app.globalData.user.id
      },
      success: function(r) {
        if (r.data.code == 200) {
          var list = r.data.data;
          for (var i of list) {
            if (i.talkType != 0) {
              i.src = i.src + "?x-oss-process=video/snapshot,t_1000,f_jpg,w_30,h_30,m_fast";
            }
            i.showMessageDate = util.formatDate(i.messageDate);
          }
          if (reach&&list.length == 0) {
            wx.showToast({
              title: '没有更多记录了',
              icon: 'none',
              duration: 3000
            })
          }
          that.setData({
            commentList: that.data.commentList.concat(list)
          })
        } else {
          wx.showToast({
            title: '获取评论列表出了点问题,请稍后重试',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络连接断开,获取评论列表失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  //返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  //键入文字
  focusInput: function(e) {
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    if (cursor > 50) {
      value = value.substr(0, 50);
    }
    this.setData({
      content: value
    })
  }, //关闭输入框
  closeFocus: function() {
    this.setData({
      focus: false
    })
  },
  //弹出输入框
  openFocus: function(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      focus: true,
      index: index
    })
  },
  //立刻回复
  comment: function(e) {
    var content = e.currentTarget.dataset.content;
    this.setData({
      content: '',
      focus: false
    })
    var index = this.data.index;
    var comment = this.data.commentList[index];
    wx.request({
      url: app.globalData.requestUrl + 'replayComment',
      method: 'POST',
      data: {
        modelId: comment.modelId,
        talkId: comment.extent2,
        userId: app.globalData.user.id,
        replay: comment.extent1,
        ohterContent: comment.message,
        content: content
      }
    })
  }
})